import ajax from "./../../api";

const state = {
  page_size: 5,
  current_page: 1,
  total_size: 0,
  table_data: [],
  search_param: {}
};

var getters = {};

const actions = {
  load_table_data(context) {
    return ajax.auth_group
      .list({
        current_page: context.state.current_page,
        page_size: context.state.page_size,
        search_param: context.state.search_param
      })
      .then(response => {
        if (response.code == 1) {
          context.commit("set_table_data", response.data);
          return true;
        }
        return false;
      })
      .catch(e => {
        console.log(e);
        return false;
      });
  },
  set_page_size(context, page_size) {
    context.commit("set_page_size", page_size);
  },
  set_current_page(context, current_page) {
    context.commit("set_current_page", current_page);
  },
  set_search_param(context, search_param) {
    context.commit("set_search_param", search_param);
  }
};

const mutations = {
  set_page_size(state, page_size) {
    state.page_size = page_size;
  },
  set_current_page(state, current_page) {
    state.current_page = current_page;
  },
  set_search_param(state, search_param) {
    state.search_param = search_param;
  },
  set_table_data(state, payload) {
    state.table_data = payload.records;
    state.total_size = payload.total;
  }
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
};
